import json
import os


# 保存session
def save_storage_state(context, num):
    print(f"浏览器{num} 保存session")
    session_storage = context.storage_state()
    # 创建文件
    if not os.path.exists(f"D:/chromeFile/session"):
        os.makedirs(f"D:/chromeFile/session")
    file_name = f"D:/chromeFile/session/{num}.json"
    with open(file_name, "w") as file:
        json.dump(session_storage, file)


# 获取session
def get_storage_state(num):
    print(f"浏览器{num} 获取session")
    file_name = f"D:/chromeFile/session/{num}.json"
    # 判断文件是否存在
    storage_state = {}
    if os.path.exists(file_name):
        with open(file_name, 'r') as file:
            storage_state = json.load(file)
    if not storage_state:
        print("session 无数据")
    return storage_state
